﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ODataRegression.Framework.Core.TestOM;
using ODataRegression.Framework.Core.MetaDataOM;
using ODataRegression.Framework.Core.Shared;
using System.Text.RegularExpressions;
using System.ComponentModel;
using System.Xml;

using ODataTestFrameworkCore.shared;

namespace ODataRegression.Framework.Core.Factories
{
    public class ODataFilterTestFactory : ODataTestFactoryBase
    {
        //int maxTestGenerationCount;
        //int generatedTestCount;
        //Regex verificationToken;
        public override List<ODataTest> CreateTestList()
        {
            //List<ODataTest> testList = new List<ODataTest>();
            EntityBaseURL = entitySet.URL;
            QueryURLBase = EntityBaseURL;// +"?$filter=";
            testName = ODataTestNames.Filter;
            testTile = "Verify that $filter is working for selected entity";
            Dictionary<string, string> dictTable = new Dictionary<string, string>();
            //verificationToken = new Regex("<m:properties>");
            // If test type not defined in config then return null
            if (this.ISAvtiveTest)
            {
                QueryURLBase = URLMappers.ApplyConfigMappingActions(QueryURLBase);
                maxTestGenerationCount = GetMaxTestGenerationCount();
                foreach (ODataEntityProperty item in entitySet.Entity.Properties)
                {

                    if (QueryURLBase.Contains("$filter"))
                    {

                        //If URL already contains $filter then just append 

                        string filter = QueryURLBase.Substring(QueryURLBase.IndexOf("$filter"));
                        filter = filter.Split('&')[0];

                        FinalQueryURL = QueryURLBase.Replace(filter, filter + " and " + item.Name + " ne null ") + "&$top=1";

                    }
                    else
                    {
                        FinalQueryURL = QueryURLBase + "?$filter=" + item.Name + " ne null &$top=1";
                    }


                     base.CreateTestList();
                    //Test only limited set of property for time being 
                    //TODO- Need to make it configurable 

                    generatedTestCount++;
                    if (generatedTestCount >= maxTestGenerationCount)
                    {
                        break;
                    }
                }
                

                //dictTable = GetFirstResponse(QueryURLBase);
                //generatedTestCount = 0;
                //if (dictTable != null)
                //{
                //    foreach (ODataEntityProperty item in entitySet.Entity.Properties)
                //    {
                //        //TODO
                //    }
                //}
            }

            return testList;
        }

        public ODataFilterTestFactory(string serviceRoot, string ServiceName, ODataEntitySet entityset )
            : base(serviceRoot, ServiceName, entityset )
        {
            //this.entitySet = entityset;
            //this.ServiceName = ServiceName;
            //this.serviceRoot = serviceRoot;
            //this.testConfig = testConfig;

        }
    }
}
